package com.lib.base.utils

import android.app.Activity
import com.lib.base.base.BaseActivity

/**
 * activity管理
 */
class ActivityUtils private constructor() {

    companion object {
        val instance by lazyOf(ActivityUtils())
    }


    /*activity集合,用来统一管理activity*/
    private var activityList = mutableListOf<Activity>()


    /*添加activity*/
    fun addActivity(activity: BaseActivity) {
        activityList.add(activity)
    }

    /*移除某个activity*/
    fun finishActivity(activity: BaseActivity) {
        if (activityList.contains(activity)) {
            activityList.remove(activity)
            activity.finish()
        }
    }

    /**移除所有的 activity*/
    fun finishAll() {
        activityList.filter { activity -> !activity.isFinishing }.forEach { activity: Activity -> activity.finish() }
        android.os.Process.killProcess(android.os.Process.myPid())
    }

    /**清除掉除了自己之外的 activity*/
    fun finishAllButNotMe(activity: Activity) {
        activityList.filter { ac -> !ac.isFinishing && ac != activity }
            .forEach { activity: Activity -> activity.finish() }
    }

}